//
//  HJChatMessageVoiceCell.m
//  XWC
//
//  Created by Hello on 2022/10/25.
//

#import "HJChatMessageVoiceCell.h"
#import "HJChatMessageVoiceLayout.h"
#import "HJChatMessageLayout.h"
#import "HJChatMessageModel.h"
#import "UIView+TUILayout.h"
#import "TUIDefine.h"

@interface HJChatMessageVoiceCell()

@property (nonatomic, weak) UILabel *durationLabel;

@end


@implementation HJChatMessageVoiceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
      
        FLAnimatedImageView *animatedImageView = [[FLAnimatedImageView alloc] init];
        animatedImageView.image = [UIImage imageNamed:@"playVoice"];
        [self.bubbleView addSubview:animatedImageView];
        _animatedImageView = animatedImageView;
 
        UILabel *durationLabel = [UILabel addToView:self.bubbleView];
        durationLabel.font = UIFontMake(14);
        durationLabel.textColor = UIColor.tj_333333Color;
        _durationLabel = durationLabel;

        UIImageView *voiceReadPointView = [UIImageView addToView:self.contentView];
        voiceReadPointView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.8];
        voiceReadPointView.frame = CGRectMake(0, 0, 8, 8);
        voiceReadPointView.hidden = YES;
        [voiceReadPointView.layer setCornerRadius:voiceReadPointView.frame.size.width/2];
        [voiceReadPointView.layer setMasksToBounds:YES];
        _voiceReadPointView = voiceReadPointView;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    HJChatMessageVoiceLayout *voiceFactory = (HJChatMessageVoiceLayout *)self.messageLayout;
 
    self.durationLabel.mm_sizeToFitThan(10,TVoiceMessageCell_Duration_Size.height);
    
    self.animatedImageView.mm_width(35).mm_height(40).mm_top((voiceFactory.containerViewRect.size.height - voiceFactory.voiceImageSize.height) * 0.5);
    
    if (voiceFactory.isSelfSend) {
        self.animatedImageView.mm_right(voiceFactory.stretchInsets.right);
        self.durationLabel.mm_left(self.animatedImageView.mm_x - self.durationLabel.mm_w - 3);
    } else {
        self.animatedImageView.mm_left(voiceFactory.stretchInsets.right);
        self.durationLabel.mm_left(self.animatedImageView.mm_maxX+ 3);
        
        self.voiceReadPointView.mm__centerY(self.containerView.mm_y).mm_left(self.bubbleView.mm_maxX - 6);
    }

    self.durationLabel.mm_centerY = self.animatedImageView.mm_centerY;

}

- (void)setMessageLayout:(HJChatMessageLayout *)messageLayout {
    [super setMessageLayout:messageLayout];
    

    HJChatMessageVoiceLayout *voiceLayout = (HJChatMessageVoiceLayout *)messageLayout;
    if (voiceLayout.duration > 0) {
        _durationLabel.text = [NSString stringWithFormat:@"%d\"", voiceLayout.duration];
    } else {
        _durationLabel.text = @"1\"";
    }

    voiceLayout.animatedImageView = _animatedImageView;
    if (voiceLayout.isPlaying) {
        self.animatedImageView.animatedImage = voiceLayout.animatedImage;
        [_animatedImageView startAnimating];
    } else {
        [_animatedImageView stopAnimating];
    }

    if (voiceLayout.isSelfSend) {
        self.voiceReadPointView.hidden = YES;
        _durationLabel.textAlignment = NSTextAlignmentRight;
    } else {
        self.voiceReadPointView.hidden = messageLayout.messageModel.voiceRead.boolValue;
        _durationLabel.textAlignment = NSTextAlignmentLeft;
    }

}

- (void)dealloc {
    NSLog(@"%s", __func__);
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
